package com.aaa.vip.service.impl;

import com.aaa.common.entity.UserAddress;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.util.TokenUtil;
import com.aaa.common.vo.Page;
import com.aaa.vip.dao.UserAddressDao;
import com.aaa.vip.service.UserAddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (UserAddress)表服务实现类
 *
 * @author makejava
 * @since 2025-09-20 09:27:30
 */
@Service("userAddressService")
public class UserAddressServiceImpl implements UserAddressService {
    @Resource
    private UserAddressDao userAddressDao;
    @Resource
    private HttpServletRequest request;

    /**
     * 查询默认地址
     *
     * @return
     */
    @Override
    public UserAddress queryDefaultAddress() {
        String userId = TokenUtil.getUserId(request);
        UserAddress userAddresses = userAddressDao.queryDefaultAddress(Integer.valueOf(userId));
        return userAddresses;
    }


    @Override
    public Integer selectCode(String country, String province) {
        return userAddressDao.selectCode(country, province);
    }

    /**
     * 通过用户id查询地址
     *
     * @param
     * @return
     */
    @Override
    public List<UserAddress> selectAllAddress() {
        String userId = TokenUtil.getUserId(request);
        return userAddressDao.selectAllAddress(Integer.valueOf(userId));
    }


    /**
     * 通过ID查询单条数据
     *
     * @param addressId 主键
     * @return 实例对象
     */
    @Override
    public UserAddress queryById(Integer addressId) {
        return this.userAddressDao.queryById(addressId);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<UserAddress> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        UserAddress userAddress = page.getData();
        return new PageInfo(userAddressDao.queryAllByLimit(userAddress));
    }

    /**
     * 新增数据
     *
     * @param userAddress 实例对象
     * @return 实例对象
     */
    @Override
    public UserAddress insert(UserAddress userAddress) {
        String userId = TokenUtil.getUserId(request);
        userAddress.setUserId(Integer.valueOf(userId));
        this.userAddressDao.insert(userAddress);
        return userAddress;
    }

    /**
     * 修改数据
     *
     * @param userAddress 实例对象
     * @return 实例对象
     */
    @Override
    public UserAddress update(UserAddress userAddress) {
        if (userAddress.getFlag().equals("0")) {
            String userId = TokenUtil.getUserId(request);
            List<UserAddress> userAddresses = userAddressDao.selectAllAddress(Integer.valueOf(userId));
            for (UserAddress address : userAddresses) {
                address.setFlag("1");
                this.userAddressDao.update(address);
            }
        }
        this.userAddressDao.update(userAddress);
        return this.queryById(userAddress.getAddressId());
    }

    /**
     * 通过主键删除数据
     *
     * @param addressId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer addressId) {
        return this.userAddressDao.deleteById(addressId) > 0;
    }
}
